package com.modudf.common;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexUtils
{
  private static RegexUtils instance = null;
  
  public static final synchronized RegexUtils getInstance()
  {
    if (instance == null) {
      instance = new RegexUtils();
    }
    return instance;
  }
  
  public boolean match(String regex, String value)
  {
    if ((value == null) || (value.trim().length() <= 0)) {
      return false;
    }
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(value);
    return m.matches();
  }
  
  public boolean is_number_digit_unsign(String value)
  {
    return match(get_number_digit_unsign(), value);
  }
  
  public boolean is_number_digit_positive(String value)
  {
    return match(get_number_digit_positive(), value);
  }
  
  public boolean is_number_digit_negative(String value)
  {
    return match(get_number_digit_negative(), value);
  }
  
  public boolean is_number_float_unsign(String value)
  {
    return match(get_number_float_unsign(), value);
  }
  
  public boolean is_number_float_positive(String value)
  {
    return match(get_number_float_positive(), value);
  }
  
  public boolean is_number_float_negative(String value)
  {
    return match(get_number_float_negative(), value);
  }
  
  public boolean is_letter(String value)
  {
    return match(get_letter(), value);
  }
  
  public boolean is_letter_lower(String value)
  {
    return match(get_letter_lower(), value);
  }
  
  public boolean is_letter_upper(String value)
  {
    return match(get_letter_upper(), value);
  }
  
  public boolean is_email(String value)
  {
    return match(get_email(), value);
  }
  
  public String get_letter()
  {
    return "^[a-zA-Z]+$";
  }
  
  public String get_email()
  {
    return "^[a-z0-9_]+\\.?[a-z0-9_]+@[a-z0-9_]+\\.[a-z]{2,4}$";
  }
  
  public String get_letter_lower()
  {
    return "^[a-z]+$";
  }
  
  public String get_letter_upper()
  {
    return "^[A-Z]+$";
  }
  
  public String get_number_digit_unsign()
  {
    return "^\\d+$";
  }
  
  public String get_number_digit_positive()
  {
    return "^\\+?\\d+$";
  }
  
  public String get_number_digit_negative()
  {
    return "^\\-\\d+$";
  }
  
  public String get_number_float_unsign()
  {
    return "^[0-9]*\\.?[0-9]*$";
  }
  
  public String get_number_float_positive()
  {
    return "^\\+?[0-9]*\\.?[0-9]*$";
  }
  
  public String get_number_float_negative()
  {
    return "^\\-[0-9]*\\.?[0-9]*$";
  }
}

